<template>
  <el-dialog :close-on-click-modal="false" append-to-body :title="title" :visible.sync="visible" width="800px">
    <div class="add-form">
      <el-form ref="form" :model="form" label-width="120px" :rules="rules" :disabled="type === 'view'">
        <el-form-item label="后台单号" prop="xmbzNo" :rules="[{required: !this.form.platformOrderId,validator: validateOrderId,trigger: 'blur'}]">
          <el-input class="w-200" v-model.trim="form.xmbzNo" size="small" placeholder="后台单号" clearable :disabled="type == 'edit'"></el-input>
        </el-form-item>
        <el-form-item label="平台单号" prop="platformOrderId" :rules="[{required: !this.form.xmbzNo, validator: validateOrderId,trigger: 'blur'}]">
          <el-input class="w-200" v-model.trim="form.platformOrderId" size="small" placeholder="平台单号" clearable :disabled="type == 'edit'"></el-input>
        </el-form-item>
        <el-form-item label="咨询类型" prop="type">
          <XmSelect ref="typeSelect" v-model="form.type" size="small" placeholder="咨询类型" :options="typeList" clearable filterable></XmSelect>
        </el-form-item>
        <el-form-item label="详细协助内容" prop="content">
          <el-input type="textarea" rows="5" v-model.trim="form.content" size="small" placeholder="详细协助内容" clearable></el-input>
        </el-form-item>
        <el-form-item label="图片附图:" prop="title">
          <div style="margin-bottom: 15px;">
            <el-upload action="/api/xmbz-goods/file/upload" list-type="picture-card" :on-success="addabnormalSuccess" :limit="10" :file-list="form.pics" :on-remove="addabnormalRemove">
              <i class="el-icon-plus"></i>
            </el-upload>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="submit" v-show="type !== 'view'" :loading="loading">{{ loading ? '提交中...' :
          '提交'
      }}</el-button>
    </span>

  </el-dialog>
</template>

<script>
import { saveService, updateService } from '@/api/workorder/consultCustomerService'
import {
  getCacheData, 
} from "@/const/cacheData"

export default {
  components: {
  },
  data() {
    return {
      visible: false,
      loading: false,
      type: "add",
      form: {
        "xmbzNo": "",
        "platformOrderId": "",
        "type": "",
        "content": "",
        "pics": []
      },
      typeList: [],
      rules: {
        type: [{
          required: true,
          message: "请选择咨询类型",
          trigger: "change"
        }],
        content: [{
          required: true,
          message: "请填写协助内容",
          trigger: "blur"
        }],
      },
      orderData: {}
    }
  },
  methods: {
    validateOrderId(rule, value, callback) {
      if (!this.form.xmbzNo && !this.form.platformOrderId) {
        return callback(new Error("后台单号或平台单号必填一个！"));
      }
      callback()
    },
    show({ type, data, orderData }) {
      this.clearForm()
      this.type = type
      this.orderData = orderData || {}
      this.visible = true
      if (orderData) {
        this.form.xmbzNo = orderData.xmbzNo
        this.form.platformOrderId = orderData.platformOrderId
      }
      if (data) {
        this.form = {
          ...data,
        }
        try {
          this.form.pics = JSON.parse(data.extJson).img.map(item => {
            return {
              url: item
            }
          })
        } catch (error) {
          console.error(error)
          this.form.pics = []
        }
      }
      
    },
    clearForm() {
      this.form = {
        "xmbzNo": "",
        "platformOrderId": "",
        "type": "",
        "content": "",
        "pics": []
      }
      this.$refs.form && this.$refs.form.clearValidate()
    },
    submit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          const request = this.type === 'add' ? saveService : updateService
          request(this.getParams()).then(res => {
            if (res.data.code === 200) {
              this.$message({
                type: 'success',
                message: `提交成功！`
              })
              this.visible = false
              this.$emit('submit')
            } else {
              this.$message({
                type: 'error',
                message: res.data.msg
              })
            }
          }).finally(() => {
            this.loading = false
          })
        }
      })
    },
    getParams() {
      const params = {
        id: this.type === 'add' ? undefined : this.form.id,
        "xmbzNo": this.form.xmbzNo,
        "platformOrderId": this.form.platformOrderId,
        "type": this.form.type,
        "typeName": this.$refs.typeSelect.getSelectedLabel(),
        "content": this.form.content,
        "extJson": JSON.stringify({ 'img': this.form.pics.map(item => item.url) })
      }
      return params
    },
    //  添加投诉图片列表
    addabnormalSuccess (response) {
      this.form.pics.push({
        url: response.data
      })
    },
    //  删除投诉图片列表
    addabnormalRemove (data) {
      this.form.pics.map((item, index) => {
        if (item.uid == data.uid) {
          this.form.pics.splice(index, 1)
        }
      })
    },
  },
  computed: {
    title() {
      const textMap = {
        'add': '新 增',
        'edit': '编 辑',
        'view': '查 看',
      }
      return textMap[this.type]
    }
  },
  async created() {
    const list = await getCacheData('ask_service_type')
    this.typeList = list.map(item => {
      return {
        ...item,
        value: item.dictKey,
        label: item.dictValue,
      }
    })
  },
}
</script>

<style lang="scss" scoped>
.add-form {}
</style>
